package com.factory.rental.service.impl;

import com.factory.rental.model.Tenant;
import com.factory.rental.dto.TenantDTO;
import com.factory.rental.repository.TenantRepository;
import com.factory.rental.service.TenantService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class TenantServiceImpl implements TenantService {

    private final TenantRepository tenantRepository;

    @Autowired
    public TenantServiceImpl(TenantRepository tenantRepository) {
        this.tenantRepository = tenantRepository;
    }

    @Override
    public Tenant saveTenant(TenantDTO tenantDTO) {
        Tenant tenant = new Tenant();
        BeanUtils.copyProperties(tenantDTO, tenant);
        return tenantRepository.save(tenant);
    }

    @Override
    public Optional<Tenant> getTenantById(Long id) {
        return tenantRepository.findById(id);
    }

    @Override
    public Optional<Tenant> getTenantByIdCard(String idCard) {
        return tenantRepository.findByIdCard(idCard);
    }

    @Override
    public Optional<Tenant> getTenantByPhone(String phone) {
        return tenantRepository.findByPhone(phone);
    }

    @Override
    public List<Tenant> getTenantsByName(String name) {
        return tenantRepository.findByNameContaining(name);
    }

    @Override
    public List<Tenant> getTenantsByStatus(Tenant.TenantStatus status) {
        return tenantRepository.findByStatus(status);
    }

    @Override
    public List<Tenant> getBlacklistedTenants() {
        return tenantRepository.findByBlacklisted(true);
    }

    @Override
    public Tenant updateTenant(Long id, TenantDTO tenantDTO) {
        Optional<Tenant> existingTenant = tenantRepository.findById(id);
        if (existingTenant.isPresent()) {
            Tenant tenant = existingTenant.get();
            BeanUtils.copyProperties(tenantDTO, tenant, "id", "createdAt");
            return tenantRepository.save(tenant);
        }
        throw new RuntimeException("Tenant not found with id: " + id);
    }

    @Override
    public void deleteTenant(Long id) {
        tenantRepository.deleteById(id);
    }

    @Override
    public void blacklistTenant(Long id, boolean blacklist) {
        Optional<Tenant> existingTenant = tenantRepository.findById(id);
        if (existingTenant.isPresent()) {
            Tenant tenant = existingTenant.get();
            tenant.setBlacklisted(blacklist);
            tenantRepository.save(tenant);
        } else {
            throw new RuntimeException("Tenant not found with id: " + id);
        }
    }

    @Override
    public Page<Tenant> getAllTenants(Pageable pageable, String keyword) {
        if (!StringUtils.hasLength(keyword)) {
            return tenantRepository.findAll(pageable);
        }
        return tenantRepository.findByNameContainingOrPhoneContainingOrIdCardContaining(keyword, keyword, keyword, pageable);
    }
}